import {
	getUserList,
	setUserList
} from '@/common/localStorage/user.js'


let maxLen = 9;
export default {
	namespaced: true,
	state: {
		// 这是全部的用户列表
		userList: getUserList(),		
	},
	getters: {
		// 当前用户 this.$store.getters['user/currUser']
		currUser: state => {
			return state.userList[0];
		}
	},
	mutations: {
		addUserList(state, obj) {		
			if (obj && obj.IdentityNo) {
				let list = getUserList();
				for (let i = 0; i < list.length; i++) {
					if (list[i].IdentityNo === obj.IdentityNo) {
						list.splice(i, 1);
						break;
					}
				}
				list = list.slice(0, maxLen - 1);
				list.unshift(obj);
				
				state.userList = list;				
				
				setUserList(list);
			} 
		},
		// obj: index, id
		removeUser(state, obj) {			
			if (obj && typeof obj == 'object') {
				let list = getUserList();
				if (obj.index != undefined) {
					list.splice(obj.index, 1);
				} else if (obj.IdentityNo != undefined) { 
					for (let i = 0; i < list.length; i++) {
						if (list[i].IdentityNo === obj.IdentityNo) {
							list.splice(i, 1);
							break;
						}
					}
				}
				state.userList = list;
				setUserList(list);
			}
		}
	}
}
